﻿using System.Data.SQLite;
using DVDTek_Data_Object.Accessor;

namespace DVDTek_Data_Object.DataObject
{
    public class ActorDataObject
    {
        #region Members
        private int id;
        private string name;

        public int Id
        {
            get { return this.id; }
        }
        public string Name
        {
            get { return this.name; }
            set
            {
                ActorDataObject actor = ActorAccessor.Instance.Select(value);
                if ((actor == null) || (actor.Id == this.Id))
                    this.name = value;
                else if (this.Id == -1)
                {
                    this.id = actor.id;
                    this.name = value;
                }
                else
                    throw new SQLiteException("Value is already use by another record");
            }
        }
        #endregion

        #region Constructors
        public ActorDataObject()
        {
            this.id = -1;
            this.Name = string.Empty;
        }
        public ActorDataObject(string name)
        {
            this.id = -1;
            this.Name = name;
        }
        public ActorDataObject(int id, string name)
        {
            this.id = id;
            this.Name = name;
        }
        #endregion

        public ActorDataObject Copy()
        {
            return new ActorDataObject(this.Id, this.Name);
        }

        public void Update()
        {
            if (this.id == -1)
                this.id = ActorAccessor.Instance.Insert(this.Name);
            else
                ActorAccessor.Instance.Update(this.Id, this.Name);
        }

        public void Delete()
        {
            if (this.id != -1)
            {
                ActorAccessor.Instance.Delete(this.Id);
                this.id = -1;
            }
        }

        public static ActorDataObject GetActorDataObject(int id)
        {
            return ActorAccessor.Instance.Select(id);
        }
    }
}
